/*
 *    Copyright (c) 2014 Xiang Xu <xuxiang@mail.bnu.edu.cn>
 *
 *    Permission is hereby granted, free of charge, to any person
 *    obtaining a copy of this software and associated documentation
 *    files (the "Software"), to deal in the Software without
 *    restriction, including without limitation the rights to use,
 *    copy, modify, merge, publish, distribute, sublicense, and/or sell
 *    copies of the Software, and to permit persons to whom the
 *    Software is furnished to do so, subject to the following
 *    conditions:
 *
 *    The above copyright notice and this permission notice shall be
 *    included in all copies or substantial portions of the Software.
 *
 *    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 *    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 *    OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 *    NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 *    HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 *    WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 *    FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 *    OTHER DEALINGS IN THE SOFTWARE.
 */

#include "arguments.h"
#include "pclwraper.h"
#include "facade_grammar.h"
#include "facade_model.h"

#include "pcl/io/pcd_io.h"
#include "opencv2/opencv.hpp"
#include "boost/program_options.hpp"
#include "boost/progress.hpp"
#include "boost/smart_ptr.hpp"

#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <ctime>
#include <assert.h>
#include <fstream>

using namespace std;

pcl::PointCloud<pcl::PointXYZ>::Ptr facade_cloud(new pcl::PointCloud<pcl::PointXYZ>);
boost::shared_ptr<FacadeGrammar> facade_grammar;
boost::shared_ptr<FacadeModel> facade_model;

// Main function
int main(int argc, char** argv)
{
    Status status;

    // parse parameters
    boost::shared_ptr<Arguments> arguments = boost::shared_ptr<Arguments>(new
            Arguments);
    status = arguments->Parse(argc, argv);

    if(!status.IsOk())
    {
        cout << status;
        exit(-1);
    }
    else
    {
        cout << "Argments Parsed success.\n";
    }

    // fetch parameters
    double resolution = arguments->GetResolution();

    if(resolution == 0)
    {
        cout << "Resolution not set! Please set \"resolution\" option in configure.json file.\n";
    }

    cout << "1. Initialize the facade model...\n";
    facade_grammar = boost::shared_ptr<FacadeGrammar>(new FacadeGrammar);

    status = facade_grammar->ReadGrammar(arguments->GetGrammarFile());
    if (!status.IsOk())
	 {
        cout << status;
        exit(-1);
    }

    facade_model = boost::shared_ptr<FacadeModel>(new FacadeModel(facade_grammar));

    cout << "\n2. Load the facade point cloud...\n";
    PclWraper::LoadPCD(arguments->GetFacadeTransformedFile(), facade_cloud);

    cout << "\n3. Construct the facade grid ...\n";
    facade_model->LoadGridScale(arguments->GetSpatialScaleFile().c_str());

    cout << "\n4. Create grid feature from point cloud ...\n";
    boost::timer timer;
    facade_model->CreateGridFromPointCloud(facade_cloud, resolution);
    cout << "Done. Time elapsed: " << timer.elapsed() << "s.\n";

    cout << "\n5. Normalize features to [-1, 1] ...\n";
    facade_model->NormalizeFeatures();

    cout << "\n6. Render feature image...\n";
    facade_model->RenderGrds(arguments->GetGridFile());

    cout << "\n7. Save feature ascii...\n";
    facade_model->SaveGrids(arguments->GetGridFile());

    cout << "\nDone.\n";
    return 0;
}
